{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "GEN_1_pytorch.ipynb",
      "private_outputs": true,
      "provenance": [],
      "collapsed_sections": [],
      "authorship_tag": "ABX9TyOdPKn5sn0C5CCdGc9hwhRt",
      "include_colab_link": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/cxbxmxcx/GenReality/blob/master/GEN_1_pytorch.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "vPS28FDZ0Hut"
      },
      "source": [
        "import torch\n",
        "from torch.autograd import Variable\n",
        "import torch.nn as nn\n",
        "import torch.nn.functional as F\n",
        "import torch.optim as optim\n",
        "import matplotlib.pyplot as plt\n",
        "import numpy as np\n",
        "torch.manual_seed(2)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "GGUPEn1Y0Wbt"
      },
      "source": [
        "X = torch.Tensor([[1,1,1],[2,2,2],[3,3,3],[4,4,4],[5,5,5]])\n",
        "Y = torch.Tensor([1,2,3,4,5]).view(-1,1)\n",
        "X, Y"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "raW0aD1I06-E"
      },
      "source": [
        "class Simple(nn.Module):\n",
        "  def __init__(self, activation=F.relu, input_dim = 3, output_dim=1):\n",
        "    super(Simple, self).__init__()\n",
        "    self.lin1 = nn.Linear(input_dim, 2)\n",
        "    self.hidden = nn.Linear(16, 2)\n",
        "    self.lin2 = nn.Linear(2, output_dim)\n",
        "    self.activation = activation\n",
        "    self.weights_init()\n",
        "\n",
        "  def forward(self, x):\n",
        "    x = self.lin1(x)\n",
        "    x = self.activation(x)\n",
        "    x = self.hidden(x)\n",
        "    x = self.activation(x)\n",
        "    x = self.lin2(x)\n",
        "    return x\n",
        "\n",
        "  def weights_init(self):\n",
        "    for m in self.modules():\n",
        "      if isinstance(m, nn.Linear):\n",
        "        # initialize the weight tensor, here we use a normal distribution\n",
        "        m.weight.data.normal_(0, 1)\n",
        "\n",
        "model = Simple()"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "n6U1QJNd2TyQ"
      },
      "source": [
        "loss_func = nn.MSELoss()\n",
        "optimizer = optim.Adam(model.parameters(), lr=0.02)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "AD6lVT_t2fWc"
      },
      "source": [
        "Training Loop"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "7uiAmDM_2jqf"
      },
      "source": [
        "epochs = 200\n",
        "steps = X.size(0)\n",
        "for i in range(epochs):\n",
        "  for j in range(steps):\n",
        "    #data_point = np.random.randint(X.size(0))\n",
        "    x_var = Variable(X[j], requires_grad=False)\n",
        "    y_var = Variable(Y[j], requires_grad=False)\n",
        "    \n",
        "    optimizer.zero_grad()\n",
        "    y_hat = model(x_var)\n",
        "    loss = loss_func.forward(y_hat, y_var)\n",
        "    loss.backward()\n",
        "    optimizer.step()\n",
        "        \n",
        "  if i % 500 == 0:\n",
        "    error = loss.data.numpy()\n",
        "    print(f\"Epoch: {i}, Loss: {error}, \")"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "hgqUnkA-3xpE"
      },
      "source": [
        "model(torch.tensor(X[0]))\n",
        "print(X[0])"
      ],
      "execution_count": null,
      "outputs": []
    }
  ]
}